/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.evidence.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.evidence.entity.CheckDetailVo;
import com.z100h.supplychain.evidence.entity.CheckListVo;
import com.z100h.supplychain.evidence.entity.TransferEvidence;
import com.z100h.supplychain.evidence.entity.WarehouseReceipt;
import com.z100h.supplychain.evidence.facade.CheckFacade;
import com.z100h.supplychain.evidence.facade.TransferFacade;
import com.z100h.supplychain.evidence.facade.WarehousingFacade;
import com.z100h.supplychain.sku.entity.SkuAmount;

/**
 * 转库凭证Controller
 * @author fyh
 * @version 2019-01-04
 */
@Controller
@RequestMapping(value = "${adminPath}/sc/check")
public class CheckController extends BaseController {

	@Autowired
	private TransferFacade<TransferEvidence> transferFacade;

	@Autowired
	private WarehousingFacade<WarehouseReceipt> warehousingFacade;

	@Autowired
	private CheckFacade checkFacade;

	/*
	 * 供应链库存校核列表页面
	 */
	@RequestMapping(value = { "index", "" })
	public String list() {
		return "supplychain/inventoryCheck/inventoryCheckList";
	}

	/*
	 * 新增库存校核入库单
	 */
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request) {
		return "supplychain/inventoryCheck/addInventoryForm";
	}

	/*详情跳转*/
	@RequestMapping(value = "detail")
	public String detail(HttpServletRequest request,int evidenceId,String type) {
		request.setAttribute("evidenceId",evidenceId);
		request.setAttribute("type",type);
		return "supplychain/inventoryCheck/inventoryCheckDetail";
	}
	@ResponseBody
	@RequestMapping(value = "save")
	public Map<String, Object> save(HttpServletRequest request, String in, String out, String comment) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SkuAmount> arrIn = null;
		List<SkuAmount> arrOut = null;
		try {
			if (StringUtils.isNotBlank(in)) {
				String jsonIn = request.getParameter("in");
				StringEscapeUtils.escapeHtml4(jsonIn);
				arrIn = JSON.parseArray(jsonIn, SkuAmount.class);
			}
			if (StringUtils.isNotBlank(out)) {
				String jsonOut = request.getParameter("out");
				StringEscapeUtils.escapeHtml4(jsonOut);
				arrOut = JSON.parseArray(jsonOut, SkuAmount.class);
			}

			/**
			 * 报损盘点库
			 */
			String warehouseType = "cf6b44a54efc406ca1f0fe15bb325556";
			/**
			 * 1.盘点：多出来的报损盘点
			 */
			transferFacade.save(EvidenceType.CHECKTRANSFER, warehouseType, arrOut, comment);

			/**
			 * 2.盘点:少的补标准入库单
			 */
			warehousingFacade.save(EvidenceType.CHECKRECEIPT, arrIn, comment);

			map.put("message", "0");
		} catch (Throwable e) {
			map.put("message", "1");
			map.put("data", e.getMessage());
			logger.error("Controller Error CheckController -> save   " + e.getMessage());
		}
		return map;
	}

	/**
	 * 
	 * 微信采购单完成
	 * 
	 * @param request
	 * @param purchaseOrderId
	 * @return
	 * @throws Throwable
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年5月7日 上午10:48:11
	 */
	@ResponseBody
	@RequestMapping(value = "mp/save")
	public AjaxJson mpSave(HttpServletRequest request, List<SkuAmount> in, List<SkuAmount> out, String comment)
			throws Throwable {
		AjaxJson respBody = new AjaxJson();

		try {
			if (localLogin(request)) {

				/**
				 * 报损盘点库
				 */
				String warehouseType = "cf6b44a54efc406ca1f0fe15bb325556";
				/**
				 * 1.盘点：多出来的报损盘点
				 */
				transferFacade.save(EvidenceType.CHECKTRANSFER, warehouseType, out, comment);

				/**
				 * 2.盘点:少的补标准入库单
				 */
				warehousingFacade.save(EvidenceType.RECEIPT, in, comment);
			}
		} catch (Throwable e) {
			respBody.setSuccess(false);
			respBody.setMsg("采购单完成异常：" + e.getMessage());
			logger.error("mp/finish is error.", e);
		}
		return respBody;
	}

	/**
	 * 
	 * 盘点列表
	 * 
	 * @param checkListVo
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年7月9日 下午1:53:53
	 */
	@ResponseBody
	@RequestMapping(value = "findByPage")
	public Map<String, Object> findByPage(CheckListVo checkListVo, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<CheckListVo> page = checkFacade.findByPage(new Page<CheckListVo>(request, response), checkListVo);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 盘点详情
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年7月9日 下午1:54:18
	 */
	@ResponseBody
	@RequestMapping(value = "findCheckDetail")
	public List<CheckDetailVo> findCheckDetail(String evidenceId) {
		return checkFacade.findCheckDetail(evidenceId);
	}

}